%**************************************************************
% Organyzes the void fraction matrix in this format:
%                               %           JG
%                               %JL     0.3	     0.5	0.7
%                               %0.05	66.05	70.34	77.17
%                               %0.1	50.47	56.90	78.53
%                               %0.3	28.61	39.96	42.16  
% Autor: Carlos Amaral (ceamaral@hotmail.com)
% data: 06/10/2010
%**************************************************************

function [void_fraction_matrix] = generate_alpha_matrix(matrix_mean,JL,JG,num_Vfiles)
                                
JL_sorted = sort(JL);           %Sort JG array elements in ascending
j = 2;
for i=1:num_Vfiles              %Erases the repetiton of JG to create matrix    
    if (i==1)
        JL_column(j) = JL_sorted(i);
        j = j+1;
    else
        if(JL_sorted(i) ~= JL_sorted(i-1))
            JL_column(j) = JL_sorted(i);     
            j = j+1;
        end
    end    
end
JG_sorted = sort(JG);           %Sort JG array elements in ascending
j = 2;
for i=1:num_Vfiles              %Erases the repetiton of JG to create matrix    
    if (i==1)
        JG_line(j) = JG_sorted(i);
        j = j+1;
    else
        if(JG_sorted(i) ~= JG_sorted(i-1) )
            JG_line(j) = JG_sorted(i);     
            j = j+1;
        end
    end    
end

%Creates result matrix

void_fraction_matrix = zeros(length(JL_column),length(JG_line)); %prelocates the size of the final image    
void_fraction_matrix(:,1)= JL_column; %Create JL column
void_fraction_matrix(1,:)= JG_line;   %Create JG line

line = 2;
column = 2;
for i=1:num_Vfiles                     %Creates result matrix
           while(JL_column(line) ~= matrix_mean(i,2)) && (line < length(JL_column))        %Changes row if JL changes
              line = line + 1;
              column = 2;               
           end
           if(JL_column(line) == matrix_mean(i,2))
                 while (JG_line(column)~= matrix_mean(i,3)) && (column <= length(JG_line)) %Changes columns until find the matching JG
                    column = column + 1;                    
                 end    
                 if (JG_line(column) == matrix_mean(i,3))
                    void_fraction_matrix(line,column) = matrix_mean(i,1);                   %When matches a value, saves in the write position of the matrix
                 end
                 if(JG_line(column)>= max(JG_line))                                         %If JG meets end of JG array, changes line
                    line = line + 1;
                    column = 2;                 
                 end                                                       
          end
end

clear JL JG JL_column line_JG j i mean_alpha;          %apaga as variaveis temporarias


%COLOCAR NOME PARA OU WM OU US----------------------------------------------

% %export EXCEL sheet 'Void_fraction_matrix.xls'
%  if (exist('Void_fraction_matrix.xls'))            %Erases old .XLS files
%         delete('Void_fraction_matrix.xls');
%  end
% columnHeader = {'', '', 'JG'};        
% rowHeader  = { ''; ''; 'JL'};         
% xlswrite('.\Results\Void_fraction_matrix.xls', void_fraction_matrix,'Sheet1','B2');   %Writes the new .XLS files
% xlswrite('.\Results\Void_fraction_matrix.xls', columnHeader,'Sheet1','A1');
% xlswrite('.\Results\Void_fraction_matrix.xls', rowHeader,'Sheet1','A1');
% 
% fprintf('\nEXCEl file created Void_fraction_matrix.xls with void fraction related to JL and JL.\n');

return